angular.module('wechat.controllers',[])
    .controller('messageCtrl',function($scope,$state,$ionicPopup,localStorageService,messageService){
        $scope.$on('$ionicView.loaded',function(){
            console.log("加载");
            $scope.messages = messageService.getAllMessages();
            $scope.popup = {
                isPopup: false,
                index: 0
            };
        });
        $scope.onSwipeLeft=function(){
            $state.go("tab.friends");
        };
        $scope.popupMessageOptions=function(message){
            $scope.popup.index=$scope.messages.indexOf(message);
            $scope.popup.optionsPopup=$ionicPopup.show({
                templateUrl:"templates/popup.html",
                scope:$scope
            });
            $scope.popup.isPopup=true;
        };
        $scope.makeMessage=function(){
            var index=$scope.popup.index;
            var message=$scope.messages[index];
            if(message.showHints){
                message.showHints=false;
                message.noReadMessages=0;
            }else{
                message.showHints=true;
                message.noReadMessages=1;
            }
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup=false;
            messageService.updateMessage(message);
        };
        $scope.deleteMessage=function(){
            var index=$scope.popup.index;
            var message=$scope.messages[index];
            $scope.messages.splice(index,1);
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup=false;
            messageService.deleteMessageId(message.id);
            messageService.clearMessage(message);
        };
        $scope.topMessage=function(){
            var index=$scope.popup.index;
            var message=$scope.messages[index];
            if(message.isTop){
                message.isTop=0;
            }else{
                message.isTop=new Date().getTime();
            }
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup=false;
            messageService.updateMessage(message);
        };
        $scope.messageDetails=function(message){
            $state.go("messageDetail",{
                "messageId":message.id
            })
        }
    })
    .controller('friendsCtrl',function($scope,$state,userService){
        $scope.onSwipeLeft=function(){
            $state.go("tab.find");
        }
        $scope.onSwipeRight=function(){
            $state.go("tab.message");
        }
    })
    .controller('findCtrl',function($scope,$state){
        $scope.onSwipeLeft=function(){
            $state.go("tab.setting");
        }
        $scope.onSwipeRight=function(){
            $state.go("tab.friends");
        }
    })
    .controller('settingCtrl',function($scope,$state){
        $scope.onSwipeRight=function(){
            $state.go("tab.find");
        }
    })
    .controller('messageDetailCtrl',['$scope','$stateParams','messageService',
        '$ionicScrollDelegate','$timeout',function($scope,$stateParams,messageService,$ionicScrollDelegate,$timeout){
            var viewScroll=$ionicScrollDelegate.$getByHandle('messageDetailsScroll');
            $scope.doRefresh=function(){
                $scope.messageNum+=5;
                $timeout(function(){
                    $scope.messageDetails=messageService.getAmountMessageById($scope.messageNum,$stateParams.messageId);
                    $scope.$broadcast('scroll.refreshComplete');
                },200)
            }
            $scope.$on("$ionicView.loaded",function(){
                $scope.message=messageService.getMessageById($stateParams.messageId);
                $scope.message.noReadMessages=0;
                $scope.message.showHints=false;
                messageService.updateMessage($scope.message);
                $scope.messageNum=10;
                $scope.messageDetails=messageService.getAmountMessageById($scope.messageNum,$stateParams.messageId);
                $timeout(function(){
                    viewScroll.scrollBottom();
                },0);
            })

            window.addEventListener("native.keyboardshow",function(e){
                viewScroll.scrollBottom();
            })
        }])
;